// 格式话表格数据
export const render = ({ record, column }) => {
  return record[column.dataIndex] || "-";
};
// 帮助文档地址
export const helpDocUrl = () => {
  const url = 'https://cloud.gtht.com.cn/cloud_doc/dailyDocs';
  // 判断用户是否是 DocAdmin 角色
  let isDocAdmin = false;
    const rolesStr = localStorage.getItem('roles');
    if (rolesStr) {
      const roles = JSON.parse(rolesStr);
      isDocAdmin = Array.isArray(roles) && roles.some(item => item.role_name === 'DocAdmin');
    }
   let encodedUserInfo;
  encodedUserInfo = encodeURI(encodeURI(JSON.stringify({isDocAdmin})))
  return  `${url}?info=${encodedUserInfo}`;
};
// 云机规格配置
export const VMSpecsFormat = (row = { disks: [] }) => {
  let res = `CPU型号 ${row?.cpuBrand}<br/>`;
  res += `&emsp;&emsp;规格 ${row?.vcpu} vCPU ( 每插槽 ${row?.socket}核 x  ${row?.corePerSocket} 插槽) | ${row?.memory}GB<br/>`;
  // 处理磁盘信息
  if (Array.isArray(row.disks) && row.disks.length > 0) {
    res += '&emsp;&emsp;磁盘 ';
    row.disks.forEach((disk, index) => {
      // const dk = `#0${index + 1} ${disk.type || '未知类型'} ${disk.size || 0}GB`;
      const dk = `#0${index + 1} ${disk|| 0}`;
      res += `${index === 0 ? dk : '&nbsp;&emsp;&emsp;&emsp;&emsp;' + dk}`;
      if (index < row.disks.length - 1) {
        res += '<br/>';
      }
    });
    res += '<br/>';
  } else {
    res += '&emsp;&emsp;磁盘 无<br/>';
  }

  return res;
};
// 请求参数
export const queryKey = () => {
  const id = window.location.pathname?.split("detail/")?.[1];
  const resourceId = window.location.href?.split("resId=")?.[1]?.split("&")?.[0]
  console.log('resourceId', window.location.href?.split("resId=")?.[1]?.split("&")?.[0]);
  const menu = window.location.href?.split("menu=")?.[1]?.split("&")?.[0];
  const clusterUuid = window.location.href?.split("&uuid=")?.[1]?.split("&menu=")?.[0];
  console.log('clusterUuid', clusterUuid);
  switch (menu) {
    case "platform":
      return { resourceId: id };
    case "cluster":
      return { resourceId, clusterUuid };
    case "host":
      return { resourceId, hostId: id };
  }
};
/**
 * 根据掩码位数获取子网掩码
 * @param {} mask
 */
function calcNWmask(mask) {
  const result = [0, 0, 0, 0];
  let tmpvar = parseInt(mask, 10);
  if (isNaN(tmpvar) || tmpvar > 32 || tmpvar < 0) {
    return null;
  }
  for (let i = 0; i < 4; i++) {
    if (tmpvar >= 8) {
      result[i] = 255;
      tmpvar -= 8;
    } else {
      result[i] = h_fillbitsfromleft(tmpvar);
      break;
    }
  }
  return result;
}
function h_fillbitsfromleft(num) {
  if (num >= 8) {
    return 255;
  }
  let bitpat = 0xff00;
  while (num > 0) {
    bitpat = bitpat >> 1;
    num--;
  }
  return bitpat & 0xff;
}
export function calNBFL(ipAddress) {
  const result = {
    mask: "",
    firstadr: "",
    lastadr: "",
    numofaddr: 0,
    bcast: "",
  };
  const arr = ipAddress.split("/");
  const ip = arr[0].split(".");
  const mask = arr[1];

  ip.forEach((item) => {
    const tmpvar = parseInt(item, 10);
    if (isNaN(tmpvar) || tmpvar > 255 || tmpvar < 0) {
      // eslint-disable-next-line no-new
      new Error("IP 错误");
    }
  });

  // 获取子网掩码
  const snm = calcNWmask(mask);
  if (!snm) {
    // error
    throw new Error("掩码位数异常");
  }
  result.mask = snm.join(".");
  const tmpvar = parseInt(mask, 10);

  const firstadr = [];
  const lastadr = [];
  if (tmpvar === 31) {
    result.numofaddr = 2;
    for (let i = 0; i < 4; i++) {
      firstadr.push(ip[i] & snm[i]);
    }

    for (let i = 0; i < 4; i++) {
      lastadr.push(ip[i] | (~snm[i] & 0xff));
    }
    result.firstadr = firstadr.join(".");
    result.lastadr = lastadr.join(".");
    return result;
  }
  if (tmpvar === 32) {
    result.numofaddr = 1;
    for (let i = 0; i < 4; i++) {
      firstadr.push(ip[i]);
    }
    result.firstadr = firstadr.join(".");
    return result;
  }
  // 获取可用IP数量
  result.numofaddr = Math.pow(2, 32 - tmpvar) - 2;
  // 获取广播地址
  const bcast = [];
  for (let i = 0; i < 4; i++) {
    bcast.push(ip[i] | (~snm[i] & 0xff));
  }
  result.bcast = bcast.join(".");
  //
  for (let i = 0; i < 4; i++) {
    firstadr.push(i === 3 ? parseInt(ip[i]) + 1 : ip[i]);
  }
  result.firstadr = firstadr.join(".");

  for (let i = 0; i < 4; i++) {
    lastadr.push(bcast[i] | (~snm[i] & 0xff));
  }
  result.lastadr = lastadr.join(".");
  return result;
}

// 格式化网络VPC创建
export function formatVpcCreateParams(platform, formdata, list=[]) {
  if (!formdata) return;
  const {
    name,
    type,
    vlanId,
    resourceId,
    physicalNetwork,
    isShared,
    isExternal,
  } = formdata;
  const vds = list?.map((item) => ({ key: item.uuid, value: item.vdsUuid }));
  switch (platform) {
    case "vmware":
      return { resourceId, name, vlanId, type, vds };
    case "easystack":
      return { resourceId, name, vlanId, type, isShared,isExternal,physicalNetwork };
    case "smartx":
      return { resourceId, name, vlanId, type, vds};
    case "huaweistack":
      return { resourceId, name, type, physicalNetwork };
  }
}
